package org.eduapp.trains.utils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * 
 * @author Zhao.Xiang
 * @date：2010-5-19
 *
 */
public class UtilsBeanCopy {
	
	public static Object beanValuesCopy(Object src, Object dst){
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(src.getClass());
			PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
			Method get = null;
			Method set = null;
			for (int i = 0; i < properties.length; i++) {
				try{
					get = properties[i].getReadMethod();
					if(properties[i].getWriteMethod() == null){
						continue;
					}
					set = dst.getClass().getMethod(properties[i].getWriteMethod().getName(), new Class[]{properties[i].getPropertyType()});
					if(set!=null && get!=null){
						Object oval = get.invoke(src, new Object[]{});
						if(oval != null){ 
							if(oval.getClass().getName().indexOf("Integer") > -1 && "0".equals(oval.toString())){
								continue;
							}
							set.invoke(dst, new Object[]{get.invoke(src, new Object[]{})});
						}
						set = null;
						get = null;
					}
				}catch(Throwable e){
					continue;
				}
			}
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} 
		return dst;
	}
	
	public static void main(String[] args){
	}
	
}
